/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import classes.Categoria;
import classes.ToDo;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.PersistenceException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.CategoriaJpaController;
import jpa.ToDoJpaController;
import utils.JPAUtil;

/**
 *
 * @author Igor
 */
@WebServlet(name = "CreateToDo", urlPatterns = {"/CreateToDo"})
public class CreateToDo extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        String destiny = "index.jsp";
        
        SimpleDateFormat toDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        
        String description = request.getParameter("descriptionToDo");
        Date start = Calendar.getInstance().getTime();
        Date end = Calendar.getInstance().getTime();
        String categoria = request.getParameter("category");
        String pessoa = request.getParameter("pessoa");
        String projeto = request.getParameter("projeto");
        
        try {
            start = toDateFormat.parse(request.getParameter("startToDo"));
        } catch (ParseException e) {
            destiny = "error.html";
            request.getRequestDispatcher(destiny).forward(request, response);
        }
        
        try {
            end = toDateFormat.parse(request.getParameter("endToDo"));
        } catch (ParseException e) {
            destiny = "error.html";
            request.getRequestDispatcher(destiny).forward(request, response);
        }
        
        ToDo t = new ToDo();
        t.setDescricao(description);
        t.setDataInicio(start);
        t.setDataFim(end);
        t.setNomeCategoria(categoria);
        t.setNomePessoa(pessoa);
        t.setNomeProjeto(projeto);
        t.setCompleted(false);

        try {
            (new ToDoJpaController(JPAUtil.getEMF())).create(t);
            request.setAttribute("sMessage", "Task succesfuly registered!");
        } catch (PersistenceException e) {
            destiny = "error.html"; // verificar @NotNull
            request.setAttribute("eMessage", "An error ocurred.");
        }
        //listar todas as categorias
        List<Categoria> listaCategorias =  (new CategoriaJpaController(JPAUtil.getEMF())).findCategoriaEntities();
        request.setAttribute("listaCategorias", listaCategorias);
        
        try { 
            request.getRequestDispatcher(destiny).forward(request, response);
        } 
        catch (Exception ex) {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
